package org.zhxy.core.quality.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.mybatis.spring.mapper.MapperFactoryBean;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.zhxy.common.base.BaseService;
import org.zhxy.common.config.MapperFactory;
import org.zhxy.common.response.PageResult;
import org.zhxy.common.response.Result;
import org.zhxy.common.utils.CollectionUtils;
import org.zhxy.common.utils.Constants;
import org.zhxy.entity.personnel.Teacher;
import org.zhxy.entity.quality.Classes;
import org.zhxy.entity.quality.ClassesTeacher;
import org.zhxy.entity.quality.TeacherClasses;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class ClassesService extends BaseService {


    public PageResult<Classes> getClassesList(Map<String, Object> map) {
        List<Classes> list = null;
        long total = 0L;
        switch (currentUser().getLevel()) {
            case Constants.System.ADMIN:
                IPage<Classes> iPage = new Page<>(Integer.parseInt(map.get(Constants.Blanket.PAGE).toString()),Integer.parseInt(map.get(Constants.Blanket.SIZE).toString()));
                QueryWrapper<Classes> queryWrapper = new QueryWrapper<>();
                queryWrapper.orderByDesc(Constants.DB.START_TIME);
                IPage<Classes> result = MapperFactory.classesMapper.selectPage(iPage, queryWrapper);
                list = result.getRecords();
                total = result.getTotal();
                break;
            case Constants.System.USER:
                map.put(Constants.Blanket.PAGE,(Integer.parseInt(map.get(Constants.Blanket.PAGE).toString()) - Constants.Number.ONE) *
                        Integer.parseInt(map.get(Constants.Blanket.SIZE).toString()));
                map.put(Constants.Blanket.SIZE,Integer.parseInt(map.get(Constants.Blanket.SIZE).toString()));
                list = MapperFactory.classesMapper.getClassesList(currentMap(map));
                total = MapperFactory.classesMapper.getTotal(currentMap(map));
                break;
        }

       /* if(CollectionUtils.checkCollection(list)){
            list.forEach(classes -> {
                QueryWrapper<ClassesTeacher> classesTeacherQueryWrapper = new QueryWrapper<>();
                classesTeacherQueryWrapper.eq(Constants.DB.CLASSES_ID,classes.getId());
                classesTeacherQueryWrapper.eq(Constants.DB.TYPE,Constants.CharacterNumber.ONE);
                ClassesTeacher classesTeacher = MapperFactory.classesTeacherMapper.selectOne(classesTeacherQueryWrapper);
                if(classesTeacher != null){
                    StringBuffer sb = new StringBuffer();
                    sb.append(classesTeacher.getJyName()).append(Constants.Sign.COMMA).append(classesTeacher.getBzrName());
                    classes.setTeacherNames(sb.toString());
                }
            });
        }*/
        return new PageResult<Classes>(total,list);
    }

    public Result saveClasses(Classes classes) {
        classes.setClassesStatus(Constants.CharacterNumber.ONE);
        classes.setNewPersonCount(Constants.Number.ZERO);
        classes.setPersonCount(Constants.Number.ZERO);
        classes.setSchoolName(MapperFactory.schoolMapper.selectById(classes.getSchoolId()).getLabel());
        classes.setSubSchoolName(MapperFactory.subSchoolMapper.selectById(classes.getSubSchoolId()).getLabel());
        classes.setVocationalName(MapperFactory.vocationalMapper.selectById(classes.getVocationalId()).getLabel());
        classes.setStageName(MapperFactory.stageMapper.selectById(classes.getStageId()).getLabel());
        MapperFactory.classesMapper.insert(classes);
        return Result.SUCCESS();
    }

    public Result updateClasses(Classes classes) {
        Classes target = MapperFactory.classesMapper.selectById(classes.getId());
        target.setClassesName(classes.getClassesName());
        MapperFactory.classesMapper.updateById(target);
        return Result.SUCCESS();
    }

    public List<ClassesTeacher> handleShowTeachers(String classesId) {
        QueryWrapper<ClassesTeacher> classesTeacherQueryWrapper = new QueryWrapper<>();
        classesTeacherQueryWrapper.eq(Constants.DB.CLASSES_ID,classesId);
        classesTeacherQueryWrapper.orderByDesc(Constants.DB.TYPE);
        return MapperFactory.classesTeacherMapper.selectList(classesTeacherQueryWrapper);
    }

    public Result appointTeachers(Map<String, Object> map) {
        String classesId = map.get(Constants.Parameters.CLASSES_ID).toString();
        String jyId = map.get(Constants.Parameters.JY_ID).toString();
        String bzrId = map.get(Constants.Parameters.BZR_ID).toString();

        //更新classes_teacher表数据  这个班级之前的数据的type改为2
        QueryWrapper<ClassesTeacher> classesTeacherQueryWrapper = new QueryWrapper<>();
        classesTeacherQueryWrapper.eq(Constants.DB.CLASSES_ID,classesId);
        classesTeacherQueryWrapper.eq(Constants.DB.TYPE,Constants.CharacterNumber.ONE);
        List<ClassesTeacher> classesTeachers = MapperFactory.classesTeacherMapper.selectList(classesTeacherQueryWrapper);
        if(CollectionUtils.checkCollection(classesTeachers)){
            classesTeachers.forEach(classesTeacher -> {
                classesTeacher.setType(Constants.CharacterNumber.TWO);
                classesTeacher.setTeacherEndTime(new Date());
                MapperFactory.classesTeacherMapper.updateById(classesTeacher);
            });
        }
        //新增classes_teacher表数据
        ClassesTeacher classesTeacher = ClassesTeacher.builder()
                .classesId(classesId).teacherEndTime(null).teacherStartTime(new Date())
                .bzrId(bzrId).bzrName(MapperFactory.teacherMapper.selectById(bzrId).getNickName())
                .jyId(jyId).jyName(MapperFactory.teacherMapper.selectById(jyId).getNickName()).type(Constants.CharacterNumber.ONE)
                .build();
        MapperFactory.classesTeacherMapper.insert(classesTeacher);
        //更新teacher_classes
        QueryWrapper<TeacherClasses> teacherClassesQueryWrapper1 = new QueryWrapper<>();
        teacherClassesQueryWrapper1.eq(Constants.DB.CLASSES_ID,classesId);
        teacherClassesQueryWrapper1.eq(Constants.DB.TEACHER_ID,jyId);
        teacherClassesQueryWrapper1.eq(Constants.DB.STATUS,Constants.CharacterNumber.ONE);
        TeacherClasses teacherClasses1 = MapperFactory.teacherClassesMapper.selectOne(teacherClassesQueryWrapper1);
        if(teacherClasses1 == null){
            //新增teacher_classes
            TeacherClasses targetTeacherClasses = TeacherClasses.builder().classesId(classesId).teacherId(jyId).status(Constants.CharacterNumber.ONE).build();
            MapperFactory.teacherClassesMapper.insert(targetTeacherClasses);
        }

        QueryWrapper<TeacherClasses> teacherClassesQueryWrapper2 = new QueryWrapper<>();
        teacherClassesQueryWrapper2.eq(Constants.DB.CLASSES_ID,classesId);
        teacherClassesQueryWrapper2.eq(Constants.DB.TEACHER_ID,bzrId);
        teacherClassesQueryWrapper2.eq(Constants.DB.STATUS,Constants.CharacterNumber.ONE);
        TeacherClasses teacherClasses2 = MapperFactory.teacherClassesMapper.selectOne(teacherClassesQueryWrapper2);
        if(teacherClasses2 == null){
            //新增teacher_classes
            TeacherClasses targetTeacherClasses = TeacherClasses.builder().classesId(classesId).teacherId(bzrId).status(Constants.CharacterNumber.ONE).build();
            MapperFactory.teacherClassesMapper.insert(targetTeacherClasses);
        }
        return Result.SUCCESS();
    }
}
